//
//  RegistVC.m
//  MoreStarLiveTelecast
//
//  Created by babbage on 16/8/23.
//  Copyright © 2016年 babbage. All rights reserved.
//

#import "RegistVC.h"
#import "NavView.h"

static CGFloat const TextFieldRegistHeight = 50;//登录注册的高度

@interface RegistVC ()<UITextFieldDelegate>

@property (nonatomic, strong) NavView *navView;//导航栏

@property (nonatomic, strong) UITextField *txtPwd;//用户密码

@property (nonatomic, strong) UITextField *txtVer;//验证码

@property (nonatomic, strong) UIButton *registBtn;//注册按钮

@end

@implementation RegistVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = BackGroundColor;
    
    [self addNotification];//注册通知 主要为倒计时服务
    [self createSta];//状态栏
    [self createNav];//导航栏
    [self createUserAndPwdAndVer];//用户名和密码
    [self createRegist];//注册按钮
}
#pragma mark --- 点击事件 ---
-(void)registAction:(UIButton *)sender
{
    MJRefreshLog(@"点击了注册按钮");
}
-(void)getVer:(UIButton *)sender
{
    self.verBtn.userInteractionEnabled = NO;
    [GJCFNotificationCenter postNotificationName:StarRegistTimer object:nil userInfo:@{@"title":@"regist"}];
}
-(void)getNowTimer:(NSNotificationCenter *)sender
{
    self.verBtn.userInteractionEnabled = NO;
    self.verBtn.backgroundColor = [UIColor cyanColor];
    NSNumber *i = [[sender valueForKey:@"userInfo"] valueForKey:@"registTime"];
    NSString *str = [NSString stringWithFormat:@"再次发送(%@)",i];
    [self.verBtn setTitle:str forState:UIControlStateNormal];
    
    if ([i integerValue] == 0) {
        self.verBtn.userInteractionEnabled = YES;
        self.verBtn.backgroundColor = [UIColor yellowColor];
        [self.verBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
}
-(void)addNotification
{
    [GJCFNotificationCenter addObserver:self selector:@selector(getNowTimer:) name:RegistNowTimer object:nil];
}
-(void)dealloc
{
    [GJCFNotificationCenter removeObserver:self];
}

#pragma mark --- 状态栏 ---
-(void)createSta
{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(staBarHeight);
    }];
}
#pragma mark --- 导航栏 ---
-(void)createNav
{
    self.navView = [[NavView alloc] initWithFrame:CGRectZero];
    self.navView.backgroundColor = [UIColor whiteColor];
    self.navView.titleString = @"注册";
    self.navView.leftImgNameStr = @"backblack";
    self.navView.rightString = @"完成";
    [self.navView setupInit];
    
    __weak typeof(self) weakSelf = self;
    self.navView.rightBlock = ^(){
        MJRefreshLog(@"点击完成");
    };
    self.navView.leftBlock = ^(){
        [weakSelf dismissViewControllerAnimated:YES completion:^{
            
        }];
    };
    [self.view addSubview:self.navView];
    [self.navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top).offset(staBarHeight);
        make.left.right.mas_equalTo(self.view);
        make.height.mas_equalTo(navBarHeight);
    }];
}
#pragma mark --- 创建用户名和密码 ---
-(void)createUserAndPwdAndVer
{
    UIView *txtUserAndPwdView = [[UIView alloc] init];
    txtUserAndPwdView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:txtUserAndPwdView];
    //线条
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = SeparatorColor;
    [txtUserAndPwdView addSubview:lineView];
    //线条2
    UIView *lineView2 = [[UIView alloc] init];
    lineView2.backgroundColor = SeparatorColor;
    [txtUserAndPwdView addSubview:lineView2];
    //手机和密码和验证码
    self.txtUser = [[UITextField alloc] initWithFrame:CGRectZero];
    self.txtUser.placeholder = @"请输入您的手机号码";
    self.txtUser.delegate = self;
    self.txtUser.keyboardType = UIKeyboardTypeNumberPad;
    self.txtUser.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36, TextFieldRegistHeight)];
    self.txtUser.leftViewMode = UITextFieldViewModeAlways;
    UIImageView *imgUser = [[UIImageView alloc] init];
    imgUser.image = [UIImage imageNamed:@"user_phone"];
    imgUser.frame = CGRectMake(Space, TextFieldRegistHeight/2 - imgUser.image.size.height/2, imgUser.image.size.width, imgUser.image.size.height);
    [self.txtUser.leftView addSubview:imgUser];
    [txtUserAndPwdView addSubview:self.txtUser];
    //手机和密码和验证码
    self.txtPwd = [[UITextField alloc] initWithFrame:CGRectZero];
    self.txtPwd.placeholder = @"请输入您的密码";
    self.txtPwd.delegate = self;
//    self.txtPwd.secureTextEntry = YES;
    self.txtPwd.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36, TextFieldRegistHeight)];
    self.txtPwd.leftViewMode = UITextFieldViewModeAlways;
    UIImageView *imgPwd = [[UIImageView alloc] init];
    imgPwd.image = [UIImage imageNamed:@"user_pwd"];
    imgPwd.frame = CGRectMake(Space, TextFieldRegistHeight/2 - imgPwd.image.size.height/2, imgPwd.image.size.width, imgPwd.image.size.height);
    [self.txtPwd.leftView addSubview:imgPwd];
    self.txtPwd.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36, 36)];
    self.txtPwd.rightViewMode = UITextFieldViewModeAlways;
    [txtUserAndPwdView addSubview:self.txtPwd];
    //手机和密码和验证码
    self.txtVer = [[UITextField alloc] initWithFrame:CGRectZero];
    self.txtVer.placeholder = @"请输入验证码";
    self.txtVer.delegate = self;
    self.txtVer.keyboardType = UIKeyboardTypeNumberPad;
    self.txtVer.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 36, TextFieldRegistHeight)];
    self.txtVer.leftViewMode = UITextFieldViewModeAlways;
    UIImageView *imgUserVer = [[UIImageView alloc] init];
    imgUserVer.image = [UIImage imageNamed:@"user_auth"];
    imgUserVer.frame = CGRectMake(Space, TextFieldRegistHeight/2 - imgUserVer.image.size.height/2, imgUserVer.image.size.width, imgUserVer.image.size.height);
    [self.txtVer.leftView addSubview:imgUserVer];
    //验证码按钮
    self.verBtn = [[UIButton alloc] init];
    [self.verBtn addTarget:self action:@selector(getVer:) forControlEvents:UIControlEventTouchUpInside];
    self.verBtn.layer.cornerRadius = 5;
    self.verBtn.layer.masksToBounds = YES;
    if (![self.registTimer integerValue]) {
        self.verBtn.userInteractionEnabled = YES;
        self.verBtn.backgroundColor = [UIColor yellowColor];
        [self.verBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    }else{
        self.verBtn.userInteractionEnabled = NO;
        self.verBtn.backgroundColor = [UIColor cyanColor];
        [self.verBtn setTitle:[NSString stringWithFormat:@"再次发送(%@)",self.registTimer] forState:UIControlStateNormal];
    }
    
    [self.verBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    self.txtVer.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120, TextFieldRegistHeight-Space)];
    self.txtVer.rightViewMode = UITextFieldViewModeAlways;
    [self.txtVer.rightView addSubview:self.verBtn];
    [txtUserAndPwdView addSubview:self.txtVer];
    [self.verBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(self.txtVer.rightView).offset(Space/2);
        make.bottom.right.mas_equalTo(self.txtVer.rightView).offset(-Space/2);
    }];
    //适配
    [txtUserAndPwdView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.navView.mas_bottom).offset(Space);
        make.left.and.right.mas_equalTo(0);
        make.bottom.mas_equalTo(self.txtVer.mas_bottom);
    }];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.txtPwd.mas_top);
        make.left.mas_equalTo(txtUserAndPwdView.mas_left).offset(Margin);
        make.right.mas_equalTo(txtUserAndPwdView.mas_right).offset(-Margin);
        make.height.mas_equalTo(1);
    }];
    [lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.txtPwd.mas_bottom);
        make.left.mas_equalTo(txtUserAndPwdView.mas_left).offset(Margin);
        make.right.mas_equalTo(txtUserAndPwdView.mas_right).offset(-Margin);
        make.height.mas_equalTo(1);
    }];
    [self UserMakeConstraints];
}
#pragma mark --- 用户名和密码页面布局 ---
-(void)UserMakeConstraints
{
    [self.txtUser mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.navView.mas_bottom).offset(Space);
        make.left.mas_equalTo(self.view).offset(Margin);
        make.right.mas_equalTo(self.view).offset(-Margin);
        make.height.mas_equalTo(TextFieldRegistHeight);
    }];
    [self.txtPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.txtUser.mas_bottom);
        make.left.and.right.equalTo(self.txtUser);
        make.height.equalTo(self.txtUser.mas_height);
    }];
    [self.txtVer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.txtPwd.mas_bottom);
        make.left.and.right.equalTo(self.txtPwd);
        make.height.equalTo(self.txtPwd.mas_height);
    }];
    
}
#pragma mark --- 注册 ---
-(void)createRegist
{
    self.registBtn = [[UIButton alloc] init];
    [self.registBtn addTarget:self action:@selector(registAction:) forControlEvents:UIControlEventTouchUpInside];
    self.registBtn.backgroundColor = navBarBackgroundColor;
    self.registBtn.layer.cornerRadius = 5;
    self.registBtn.layer.masksToBounds = YES;
    [self.registBtn setTitle:@"注册" forState:UIControlStateNormal];
    [self.registBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:self.registBtn];
    [self.registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.txtVer.mas_bottom).offset(Margin);
        make.left.mas_equalTo(self.view.mas_left).offset(Margin);
        make.right.mas_equalTo(self.view.mas_right).offset(-Margin);
        make.height.mas_equalTo(TextFieldRegistHeight);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
